<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:insert="~{include :: header('新增通知模板')}" />
    <th:block th:insert="~{include :: summernote-css}" />
    <style>
        .template-vars {
            margin-bottom: 15px;
        }
        .var-example {
            background: #f8f9fa;
            padding: 10px;
            border-radius: 4px;
            margin-top: 10px;
        }
    </style>
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-template-add">
            <div class="form-group">
                <label class="col-sm-2 control-label is-required">模板名称：</label>
                <div class="col-sm-10">
                    <input id="templateName" name="templateName" class="form-control" type="text" required>
                </div>
            </div>
            
            <div class="form-group">
                <label class="col-sm-2 control-label is-required">模板编码：</label>
                <div class="col-sm-10">
                    <input id="templateCode" name="templateCode" class="form-control" type="text" required>
                    <small class="help-block">唯一编码，用于程序调用</small>
                </div>
            </div>
            
            <div class="form-group">
                <label class="col-sm-2 control-label">模板类型：</label>
                <div class="col-sm-10">
                    <select name="templateType" class="form-control m-b" th:with="type=${@dict.getType('t_notice_type')}">
                        <option value="1">系统通知</option>
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                    </select>
                </div>
            </div>
            
            <div class="form-group">
                <label class="col-sm-2 control-label is-required">模板标题：</label>
                <div class="col-sm-10">
                    <input id="title" name="title" class="form-control" type="text" required>
                </div>
            </div>
            
            <div class="form-group">
                <label class="col-sm-2 control-label">发送方式：</label>
                <div class="col-sm-10">
                    <select name="sendMethod" class="form-control m-b" th:with="method=${@dict.getType('sys_notice_method')}">
                        <option value="1">站内信</option>
                        <option th:each="dict : ${method}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                    </select>
                </div>
            </div>
            
            <div class="form-group">
                <label class="col-sm-2 control-label">优先级：</label>
                <div class="col-sm-10">
                    <select name="priority" class="form-control m-b" th:with="priority=${@dict.getType('sys_notice_priority')}">
                        <option value="2">中</option>
                        <option th:each="dict : ${priority}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                    </select>
                </div>
            </div>
            
            <div class="form-group">
                <label class="col-sm-2 control-label">模板变量：</label>
                <div class="col-sm-10 template-vars">
                    <textarea name="templateVars" class="form-control" rows="3" placeholder="请输入模板变量定义，JSON格式"></textarea>
                    <div class="var-example">
                        <strong>变量示例：</strong><br>
                        {<br>
                        &nbsp;&nbsp;"userName": "用户姓名",<br>
                        &nbsp;&nbsp;"taskName": "任务名称",<br>
                        &nbsp;&nbsp;"deadline": "截止时间"<br>
                        }
                    </div>
                </div>
            </div>
            
            <div class="form-group">
                <label class="col-sm-2 control-label is-required">模板内容：</label>
                <div class="col-sm-10">
                    <input id="content" name="content" type="hidden">
                    <div class="summernote"></div>
                    <small class="help-block">使用{变量名}格式定义变量占位符，如：尊敬的{userName}，您有一个新任务：{taskName}</small>
                </div>
            </div>
            
            <div class="form-group">
                <label class="col-sm-2 control-label">状态：</label>
                <div class="col-sm-10">
                    <div class="radio-box" th:each="dict : ${@dict.getType('sys_normal_disable')}">
                        <input type="radio" th:id="${dict.dictCode}" name="status" th:value="${dict.dictValue}" th:checked="${dict.default}">
                        <label th:for="${dict.dictCode}" th:text="${dict.dictLabel}"></label>
                    </div>
                </div>
            </div>
            
            <div class="form-group">
                <label class="col-sm-2 control-label">备注：</label>
                <div class="col-sm-10">
                    <textarea name="remark" class="form-control" rows="2"></textarea>
                </div>
            </div>
        </form>
    </div>
    
    <th:block th:insert="~{include :: footer}" />
    <th:block th:insert="~{include :: summernote-js}" />
    
    <script type="text/javascript">
        var prefix = ctx + "message/template";
        
        $(function() {
            $('.summernote').summernote({
                placeholder: '请输入模板内容，使用{变量名}格式定义变量',
                height: 300,
                lang: 'zh-CN',
                followingToolbar: false,
                dialogsInBody: true,
                callbacks: {
                    onImageUpload: function(files) {
                        sendFile(files[0], this);
                    }
                }
            });
        });
        
        // 上传文件
        function sendFile(file, obj) {
            var data = new FormData();
            data.append("file", file);
            $.ajax({
                type: "POST",
                url: ctx + "common/upload",
                data: data,
                cache: false,
                contentType: false,
                processData: false,
                dataType: 'json',
                success: function(result) {
                    if (result.code == web_status.SUCCESS) {
                        $(obj).summernote('editor.insertImage', result.url, result.fileName);
                    } else {
                        $.modal.alertError(result.msg);
                    }
                },
                error: function(error) {
                    $.modal.alertWarning("图片上传失败。");
                }
            });
        }
        
        $("#form-template-add").validate({
            focusCleanup: true
        });
        
        function submitHandler() {
            if ($.validate.form()) {
                var sHTML = $('.summernote').summernote('code');
                $("#content").val(sHTML);
                $.operate.save(prefix + "/add", $('#form-template-add').serialize());
            }
        }
    </script>
</body>
</html>